home *** CD-ROM | disk | FTP | other *** search
- Path: crl.crl.com!not-for-mail
- From: bobfry@crl.com (Robert Fry)
- Newsgroups: comp.lang.c
- Subject: Re: Alignment restrictions...
- Date: 9 Jan 1996 16:00:46 -0800
- Organization: CRL Dialup Internet Access
- Message-ID: <4cuvje$48l@crl.crl.com>
- References: <DKxHxG.EK1@ridgecrest.ca.us>
- NNTP-Posting-Host: crl.com
-
- mcclung@owens.ridgecrest.ca.us (Scott McClung) writes:
-
- >Hi,
-
- >In architectures that have alignment restrictions on certain types (i.e.
- >ints aligned on 4 byte boundaries, etc.), does anyone have any clean
- >way of retrieving values that are not aligned correctly?
-
- >I was porting some code out that had statements like:
-
- >short value; /* 16 bit value */
- >char *ptr; /* 32 bit pointer, no alignment restrictions */
-
- >value = *(short *)ptr;
-
- >It worked on, say, Intel x86, or on values of ptr that happened to be
- >aligned (auto variables, in this case), but is not a general solution
- >on SPARC or other RISC architectures (MIPS and PA-RISC have similar
- >restrictions, if I recall.) The pointer in question happened to be
- >into an mmap()'ed image file, so the alignment is not guaranteed. Bus
- >errors.
-
- The last time I ran across this one, good ol' memcpy() did the trick.
- Since our compiler inlined the call, it even generated code that was
- exactly as fast as I could do in hand-coded assembly.
-
- e.g:
- memcpy(value, ptr, sizeof( value)); /* with appropriate casts, as needed. */
-
- What do others use?
-
-